package tr.edu.ozyegin.ccrg.raid.network;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class NetworkManager {

	private PType NetworkType = PType.Client;
	private boolean isNetworkTypeSet = false;
	private boolean isAddressSet = false;
	private boolean isPortSet = false;
	private boolean isStreamsCreated = false;
	/**
	 * @param args
	 */ 
	private Socket clientSocket = null;
	private ServerSocket serverSocket = null;
	private int port = 0;
	private DataOutputStream stOut = null;
	private DataInputStream stIn = null;
	
	public int getPort(){
		return port;
	}
	public NetworkManager(Socket socket){
		this.address="";
		this.port=0;
		this.NetworkType = PType.Server;
		this.createStreams(socket);
	}
	public boolean setAddress(String address){
		if(!this.isAddressSet && this.NetworkType == PType.Client){
			this.address = address;
			this.isAddressSet = true;
		}
		return !this.isAddressSet;
	}
	public boolean setPort(int port){
		if(!this.isPortSet){
			this.port = port;
			this.isPortSet = true;
		}
		return !this.isPortSet;
	}
	public String getAddress(){
		return this.address;
	}
	public Object getSocket(){
		if(this.isNetworkTypeSet){
			if(this.NetworkType == PType.Client){
				return this.clientSocket;
			}
			else{
				return this.serverSocket;
			}
		}
		return null;
	}
	public boolean createStreams(Socket sock){
		if(!this.isStreamsCreated){
			boolean outOn = false;
			try {
				this.stOut = new DataOutputStream(sock.getOutputStream());
				outOn = true;
				this.stIn = new DataInputStream(sock.getInputStream());
				this.isStreamsCreated = true;
				return true;
			} catch (IOException e) {
				if(outOn){
					this.stOut = null;
				}
			}
		}
		return false;
	}
	public boolean wirteBytes(byte[] value){
		if(this.stOut != null){
			try {
				this.stOut.write(value);
				return true;
			} catch (IOException e) {}
		}
		return false;
	}
	public boolean writeString(String value){
		if(this.stOut != null){
			try {
				this.stOut.writeUTF(value);
				return true;
			} catch (IOException e) {}
		}
		return false;
	}
	public boolean writeInt(int value){
		if(this.stOut != null){
			try {
				this.stOut.writeInt(value);
				return true;
			} catch (IOException e) {}
		}
		return false;
	}
	public boolean writeShort(short value){
		if(this.stOut != null){
			try {
				this.stOut.writeShort(value);
				return true;
			} catch (IOException e) {}
		}
		return false;
	}
	public boolean writeBool(boolean value){
		if(this.stOut != null){
			try {
				this.stOut.writeBoolean(value);
				return true;
			} catch (IOException e) {}
		}
		return false;
	}
	public boolean readBytes(byte [] buffer){
		if(this.stIn != null){
			try {
				this.stIn.read(buffer);
				return true;
			} catch (IOException e) {
			}
		}
		return false;
	}
	public String readString(){
		String result = null;
		if(this.stIn != null){
			try {
				result = this.stIn.readUTF();
			} catch (IOException e) {
			}
		}
		return result;
	}
	public int readInt(){
		int result = 0;
		if(this.stIn != null){
			try {
				result = this.stIn.readInt();
			} catch (IOException e) {
			}
		}
		return result;
	}
	public short readShort(){
		short result = 0;
		if(this.stIn != null){
			try {
				result = this.stIn.readShort();
			} catch (IOException e) {
			}
		}
		return result;
	}
	public boolean readBool(){
		boolean result = false;
		if(this.stIn != null){
			try {
				result = this.stIn.readBoolean();
			} catch (IOException e) {
			}
		}
		return result;
	}
	private String address = "";
	public boolean setPointType(PType NetworkType) throws IOException{
		if(!this.isNetworkTypeSet){
			this.NetworkType = NetworkType;
			if(this.NetworkType == PType.Client && this.isAddressSet && this.isPortSet){
				this.clientSocket = new Socket(this.address,this.port);
			}
			else if(this.NetworkType == PType.Server && this.isPortSet){
				this.serverSocket = new ServerSocket(this.port);
			}
			this.isNetworkTypeSet = true;
		}
		return !this.isNetworkTypeSet;
	}
	public NetworkManager(){
		
	}
}
